package cn.huanglei936.essay.itext;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author huanglei
 * @email lhuang@matmass.com
 * @data 2018/10/28 9:20
 */
public class ImagesToPdf {

    @Test
    public void imgToPdf() throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ImagesToPdf().createPdf(DEST);
    }

    public static final String[] IMAGES = {
            "C:\\Projects\\infomass\\matmass new\\备份\\autostandard\\images\\full\\098a9f2f28d5c54b6911a8589304c16eee655082.jpg",
            "C:\\Projects\\infomass\\matmass new\\备份\\autostandard\\images\\full\\ac6f278ae071bf253edcd7cac06710fde219bdb1.jpg",
            "C:\\Projects\\infomass\\matmass new\\备份\\autostandard\\images\\full\\f2df12a6d6b2d6e971935e67a4f09c4c6d86e60b.jpg"
    };
    public static final String DEST = "C:\\Projects\\infomass\\matmass new\\备份\\autostandard\\multiple_images.pdf";

    public void createPdf(String dest) throws IOException, DocumentException {
        Image img = Image.getInstance(IMAGES[0]);
        Document document = new Document(img);
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        for (String image : IMAGES) {
            img = Image.getInstance(image);
            document.setPageSize(img);
            document.newPage();
            img.setAbsolutePosition(0, 0);
            document.add(img);
        }
        document.close();
    }

}
